﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SystemManage_GroupPermission : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    public string roleid = "";
    protected DataTable dt = null;
    protected DataTable dt2 = null;
    protected Hashtable ht = null;
    protected Hashtable ht2 = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("100300");
        roleid = Request.QueryString["ugcode"];
        if (!IsPostBack)
        {
            CreateDataSet();
            CreatePermission(roleid);
            InitTree(TreeView1.Nodes, 0);

        }
    }

    private void CreateDataSet()
    {
        DataSet ds = new DataSet();
        ds = new BLL.USERS().P_System_Functions_Operate_List();
        dt = ds.Tables[0];
        dt2 = ds.Tables[1];
    }


    #region 权限列表
    private void CreatePermission(string roleid)
    {
        DataSet ds = new DataSet();
        ds = new BLL.USERS().GetModuleList(roleid, "GroupName");//获取目录树
        DataTable dt3 = null;
        DataTable dt4 = null;
        dt3 = ds.Tables[0];
        dt4 = ds.Tables[1];
        ht = new Hashtable();
        for(int i=0;i<dt3.Rows.Count;i++)
        {
            ht.Add(dt3.Rows[i]["FUNC_CODE"].ToString().Trim(), dt3.Rows[i]["FUNC_CODE"].ToString().Trim());
        }
        ht2 = new Hashtable();
        for (int a = 0; a < dt4.Rows.Count; a++)
        {
            ht2.Add(dt4.Rows[a]["FUNCCODE"].ToString().Trim() + dt4.Rows[a]["OPERATECODE"].ToString().Trim(), dt4.Rows[a]["FUNCCODE"].ToString().Trim() + dt4.Rows[a]["OPERATECODE"].ToString().Trim());
        }
    }
    #endregion

    #region 初始化树
    //从根节点开始递归调用显示子树 
    private void InitTree(TreeNodeCollection Nds, int parentId)
    {
        DataView dv = new DataView();
        TreeNode tmpNd, tmpNd2;
        //int intId;
        dv.Table = dt;
        dv.RowFilter = "FUNC_FATHERCODE=" + parentId;
        foreach (DataRowView drv in dv)
        {
            tmpNd = new TreeNode();
            tmpNd.Value = drv["FUNC_CODE"].ToString().Trim();
            tmpNd.Text = drv["FUNC_NAME"].ToString();
            if (ht.ContainsValue(drv["FUNC_CODE"].ToString().Trim()))
            {
                tmpNd.Checked = true;
            }
            else
            {
                tmpNd.Checked = false;
            }
            Nds.Add(tmpNd);
            //intId = Convert.ToInt32(drv["superid"].ToString());
            tmpNd.Expanded = true;
            if (drv["FUNC_LEAF"].ToString() == "1")
            {
                InitTreeOperate(tmpNd.ChildNodes, tmpNd.Value.Trim());
            }
            InitTree(tmpNd.ChildNodes, Convert.ToInt32(tmpNd.Value));
        }
    }
    #endregion

    #region 操作功能树节点

    private void InitTreeOperate(TreeNodeCollection Nds, string parentId)
    {
        DataView dv2 = new DataView();
        TreeNode tmpNd2;
        dv2.Table = dt2;
        dv2.RowFilter = "FO_FUNCCODE=" + parentId;
        foreach (DataRowView drv2 in dv2)
        {
            tmpNd2 = new TreeNode();
            tmpNd2.Value = drv2["OP_CODE"].ToString().Trim();
            tmpNd2.Text = drv2["OP_NAME"].ToString();
            tmpNd2.ToolTip = "operate";
            if (ht2.ContainsValue(parentId+drv2["OP_CODE"].ToString().Trim()))
            {
                tmpNd2.Checked = true;
            }
            else
            {
                tmpNd2.Checked = false;
            }
            Nds.Add(tmpNd2);
            tmpNd2.Expanded = true;
        }
    }
    #endregion

    #region
    //角色加入所选的权限
    private void GetAllNodeText(TreeNodeCollection tnc)
    {
        foreach (TreeNode node in tnc)
        {
            if (node.ChildNodes.Count != 0)
                GetAllNodeText(node.ChildNodes);
            //加入所选的权限
            if (node.Checked)
            {
                if (node.ToolTip == "operate")
                {
                    new BLL.USERS().P_UserGroup_Function_Operate_Update(roleid, node.Parent.Value, node.Value,1);
                }
                else
                {
                    new BLL.USERS().P_UserGroup_Function_Operate_Update(roleid, node.Value, "", 1);
                }
            }
            else
            {
                if (node.ToolTip == "operate")
                {
                    new BLL.USERS().P_UserGroup_Function_Operate_Update(roleid, node.Parent.Value, node.Value, 0);
                }
                else
                {
                    new BLL.USERS().P_UserGroup_Function_Operate_Update(roleid, node.Value, "", 0);
                }
            }
        }
    }
    #endregion
    protected void Btn_save_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("100300", "00005"))
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('无权操作')", true); return;
        }
        try
        {
            GetAllNodeText(TreeView1.Nodes);
            Share.InsertLog("100300", "00005");
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('保存成功')", true);
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('保存失败')", true);
        }

        InitTree(this.TreeView1.Nodes, 0);
    }
}
